﻿using System;

using DeviceInfoManage.DataAccessLayer;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DeviceInfoManage.BusinessLogicLayer
{
    /// <summary>
    /// 设备维护管理
    /// </summary>
    [Serializable]
    public class reservedMan
    {

        #region 字段属性
        private int _reservedid;
        private string _deviceuser;
        private string _departmentname;
        private DateTime _reservedtime;
        private string _devicemodel;
        private string _deviceserialnumber;
        private string _propertyserialnumber;
        private int _reservedpersonid;
        private string _reservedpath;
        /// <summary>
        /// 备份ID
        /// </summary>
        public int reservedId
        {
            set { _reservedid = value; }
            get { return _reservedid; }
        }
        /// <summary>
        /// 使用人
        /// </summary>
        public string deviceUser
        {
            set { _deviceuser = value; }
            get { return _deviceuser; }
        }
        /// <summary>
        /// 所属部门
        /// </summary>
        public string departmentName
        {
            set { _departmentname = value; }
            get { return _departmentname; }
        }
        /// <summary>
        /// 备份时间
        /// </summary>
        public DateTime reservedTime
        {
            set { _reservedtime = value; }
            get { return _reservedtime; }
        }
        /// <summary>
        /// 设备型号
        /// </summary>
        public string deviceModel
        {
            set { _devicemodel = value; }
            get { return _devicemodel; }
        }
        /// <summary>
        /// 设备编号
        /// </summary>
        public string deviceSerialNumber
        {
            set { _deviceserialnumber = value; }
            get { return _deviceserialnumber; }
        }
        /// <summary>
        /// 资产编号
        /// </summary>
        public string propertySerialNumber
        {
            set { _propertyserialnumber = value; }
            get { return _propertyserialnumber; }
        }
        /// <summary>
        /// 备份人
        /// </summary>
        public int reservedPersonId
        {
            set { _reservedpersonid = value; }
            get { return _reservedpersonid; }
        }
        /// <summary>
        /// 备份路径
        /// </summary>
        public string reservedPath
        {
            set { _reservedpath = value; }
            get { return _reservedpath; }
        }
        #endregion 字段属性

        #region 构造函数
        public reservedMan()
        { }
        #endregion

        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("reservedId", "reservedMan");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int reservedId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from reservedMan");
            strSql.Append(" where reservedId=@reservedId ");
            SqlParameter[] parameters = {
					new SqlParameter("@reservedId", SqlDbType.Int,4)};
            parameters[0].Value = reservedId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(reservedMan model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into reservedMan(");
            strSql.Append("deviceUser,departmentName,reservedTime,deviceModel,deviceSerialNumber,propertySerialNumber,reservedPersonId,reservedPath)");
            strSql.Append(" values (");
            strSql.Append("@deviceUser,@departmentName,@reservedTime,@deviceModel,@deviceSerialNumber,@propertySerialNumber,@reservedPersonId,@reservedPath)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@deviceUser", SqlDbType.VarChar,20),
					new SqlParameter("@departmentName", SqlDbType.VarChar,20),
					new SqlParameter("@reservedTime", SqlDbType.DateTime),
					new SqlParameter("@deviceModel", SqlDbType.VarChar,50),
					new SqlParameter("@deviceSerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@propertySerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@reservedPersonId", SqlDbType.Int,4),
					new SqlParameter("@reservedPath", SqlDbType.VarChar,50)};
            parameters[0].Value = model.deviceUser;
            parameters[1].Value = model.departmentName;
            parameters[2].Value = model.reservedTime;
            parameters[3].Value = model.deviceModel;
            parameters[4].Value = model.deviceSerialNumber;
            parameters[5].Value = model.propertySerialNumber;
            parameters[6].Value = model.reservedPersonId;
            parameters[7].Value = model.reservedPath;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(reservedMan model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update reservedMan set ");
            strSql.Append("deviceUser=@deviceUser,");
            strSql.Append("departmentName=@departmentName,");
            strSql.Append("reservedTime=@reservedTime,");
            strSql.Append("deviceModel=@deviceModel,");
            strSql.Append("deviceSerialNumber=@deviceSerialNumber,");
            strSql.Append("propertySerialNumber=@propertySerialNumber,");
            strSql.Append("reservedPersonId=@reservedPersonId,");
            strSql.Append("reservedPath=@reservedPath");
            strSql.Append(" where reservedId=@reservedId ");
            SqlParameter[] parameters = {
					new SqlParameter("@reservedId", SqlDbType.Int,4),
					new SqlParameter("@deviceUser", SqlDbType.VarChar,20),
					new SqlParameter("@departmentName", SqlDbType.VarChar,20),
					new SqlParameter("@reservedTime", SqlDbType.DateTime),
					new SqlParameter("@deviceModel", SqlDbType.VarChar,50),
					new SqlParameter("@deviceSerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@propertySerialNumber", SqlDbType.VarChar,50),
					new SqlParameter("@reservedPersonId", SqlDbType.Int,4),
					new SqlParameter("@reservedPath", SqlDbType.VarChar,50)};
            parameters[0].Value = model.reservedId;
            parameters[1].Value = model.deviceUser;
            parameters[2].Value = model.departmentName;
            parameters[3].Value = model.reservedTime;
            parameters[4].Value = model.deviceModel;
            parameters[5].Value = model.deviceSerialNumber;
            parameters[6].Value = model.propertySerialNumber;
            parameters[7].Value = model.reservedPersonId;
            parameters[8].Value = model.reservedPath;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int reservedId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from reservedMan ");
            strSql.Append(" where reservedId=@reservedId ");
            SqlParameter[] parameters = {
					new SqlParameter("@reservedId", SqlDbType.Int,4)};
            parameters[0].Value = reservedId;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public reservedMan GetModel(int reservedId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 reservedId,deviceUser,departmentName,reservedTime,deviceModel,deviceSerialNumber,propertySerialNumber,reservedPersonId,reservedPath from reservedMan ");
            strSql.Append(" where reservedId=@reservedId ");
            SqlParameter[] parameters = {
					new SqlParameter("@reservedId", SqlDbType.Int,4)};
            parameters[0].Value = reservedId;

            reservedMan model = new reservedMan();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["reservedId"].ToString() != "")
                {
                    model.reservedId = int.Parse(ds.Tables[0].Rows[0]["reservedId"].ToString());
                }
                model.deviceUser = ds.Tables[0].Rows[0]["deviceUser"].ToString();
                model.departmentName = ds.Tables[0].Rows[0]["departmentName"].ToString();
                if (ds.Tables[0].Rows[0]["reservedTime"].ToString() != "")
                {
                    model.reservedTime = DateTime.Parse(ds.Tables[0].Rows[0]["reservedTime"].ToString());
                }
                model.deviceModel = ds.Tables[0].Rows[0]["deviceModel"].ToString();
                model.deviceSerialNumber = ds.Tables[0].Rows[0]["deviceSerialNumber"].ToString();
                model.propertySerialNumber = ds.Tables[0].Rows[0]["propertySerialNumber"].ToString();
                if (ds.Tables[0].Rows[0]["reservedPersonId"].ToString() != "")
                {
                    model.reservedPersonId = int.Parse(ds.Tables[0].Rows[0]["reservedPersonId"].ToString());
                }
                model.reservedPath = ds.Tables[0].Rows[0]["reservedPath"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select reservedId,deviceUser,departmentName,reservedTime,deviceModel,deviceSerialNumber,propertySerialNumber,reservedPersonId,reservedPath ");
            strSql.Append(" FROM reservedMan ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" reservedId,deviceUser,departmentName,reservedTime,deviceModel,deviceSerialNumber,propertySerialNumber,reservedPersonId,reservedPath ");
            strSql.Append(" FROM reservedMan ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM reservedPathInfo ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet QueryDevice(reservedMan mr)
        {
            try
            {
                string queryString = "select * from reservedInfoView";
                string strSql = string.Empty;
                if (mr.departmentName != "") strSql = " and departmentName like '%" + mr.departmentName + "%'";
                if (mr.reservedPath != "") strSql += " and reservedPathName like '%" + mr.reservedPath + "%'";
                if (mr.deviceUser != "") strSql += " and deviceUser like '%" + mr.deviceUser + "%'";
                if (mr.deviceSerialNumber != "") strSql += " and deviceSerialNumber like '%" + mr.deviceSerialNumber + "%'";
                if (mr.reservedTime != DateTime.MinValue) strSql += " and reservedTime = '" + mr.reservedTime.ToShortDateString() + "'";
                if (strSql.StartsWith(" and"))
                {
                    strSql = strSql.Remove(0, 4);
                    queryString += " where" + strSql;
                }
                ///*如果用户设备了开始时间的查询条件*/
                //if (startTimeString != "")
                //    queryString += " and devicePurchaseTime>'" + Convert.ToDateTime(startTimeString) + "'";
                ///*如果用户设置了结束时间的查询条件*/
                //if (endTimeString != "")
                //    queryString += " and devicePurchaseTIme<'" + Convert.ToDateTime(endTimeString) + "'";

                DataBase db = new DataBase();
                return db.GetDataSet(queryString);
            }
            catch
            {
                return null;
            }
        }
        #endregion  成员方法
    }
}

